package com.sinder.campus_prophet.core.config;

import com.sinder.campus_prophet.core.global.ResponseMessage;
import com.sinder.campus_prophet.exception.JwglException;
import com.sinder.campus_prophet.exception.RequestLimitException;
import com.sinder.campus_prophet.exception.WxApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.text.ParseException;

/**
 * 全局异常处理器
 * @author sinder
 * @Date 2023/3/16/21:00
 **/
@RestControllerAdvice
@Configuration
@Slf4j
public class BaseExceptionConfig {

    /**
     * 全局异常处理
     */
    @ExceptionHandler(value = RuntimeException.class)
    @ResponseBody
    public ResponseMessage<Object> runtimeException(RuntimeException e) {
        log.error(e.getMessage());
        return ResponseMessage.createByErrorCodeMessage(10000, e.getMessage());
    }

    /**
     * 微信请求接口异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(value = WxApiException.class)
    @ResponseBody
    public ResponseMessage<Object> wxApiException(WxApiException e) {
        // 获得异常信息返回
        return ResponseMessage.createByErrorCodeMessage(20000, e.getMessage());
    }

    /**
     * 高频访问异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = RequestLimitException.class)
    @ResponseBody
    public ResponseMessage<Object> requestLimitException(RequestLimitException e) {
        return ResponseMessage.createByErrorCodeMessage(30000, e.getMessage());
    }

    /**
     * 教务系统访问异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = JwglException.class)
    @ResponseBody
    public ResponseMessage<Object> jwglWxception(JwglException e) {
        return ResponseMessage.createByErrorCodeMessage(40000, e.getMessage());
    }
}
